package com.test.qnews.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;

import java.util.List;

/**
 * Created by dllo on 16/1/12.
 */
public class ViewPagerAdapter extends PagerAdapter{
    private Context context;
    private   List<View> list ;

    public ViewPagerAdapter(Context context, List<View> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        if (list.size() > 1){
            return Integer.MAX_VALUE;//设置成最大，使用户看不到边界
        }
        return list.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
//        container.removeView(list.get(position));
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        int index = position % list.size();
        if (index < 0){
            index = list.size()+index;
        }
        View view = list.get(index);

        ViewParent vp = view.getParent();
        if (vp != null){
            ViewGroup group = (ViewGroup) vp;
            group.removeView(view);
        }
        container.addView(view);
        return view;
    }


}
